/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2023-11-06
 * Time: 11:16
 */
public class BinarySearchTree {

    static class TreeNode {
        private int val;
        public TreeNode right;
        public TreeNode left;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public int UsedSize;

    public TreeNode root;

    public TreeNode search(int val) {

        TreeNode cur = root;
        while (cur != null) {
            if (cur.val == val) {
                return cur;
            } else if (cur.val > val) {
                cur = cur.left;
            } else {
                cur = cur.right;
            }
        }
        return null;
    }
    public boolean insert(int key) {
        TreeNode node = new TreeNode(key);

        TreeNode cur = root;
        TreeNode parent = null;

        if (cur == null) {
            root = node;
            return true;
        }

        while (cur != null) {
            if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else {
                return false;
            }
        }

        if (parent.val > key) {
            parent.left = node;
        } else {
            parent.right = node;
        }
        UsedSize++;
        return true;
    }

    public boolean removNode(int key) {
        TreeNode cur = root;
        TreeNode parent = null;

        while (cur != null) {
            if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else {
                Remove(cur, parent);
                return true;
            }
        }
        return false;
    }

    private void Remove(TreeNode cur, TreeNode parent) {
        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            } else if (parent.left == cur) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }
        } else if (cur.right == null) {
            if (cur == root) {
                root = cur.left;
            } else if (parent.left == cur) {
                parent.left = cur.left;
            } else {
                parent.right = cur.right;
            }
        } else {
            TreeNode targetPartent = cur;
            TreeNode target = cur.right;

            while (target.left != null) {
                targetPartent = target;
                target = target.left;
            }

            cur.val = target.val;

            if (targetPartent.left == target) {
                targetPartent.left = target.right;
            } else {
                targetPartent.right = target.right;
            }
        }
    }
    public static void main(String[] args) {
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        binarySearchTree.insert(5);
        binarySearchTree.insert(3);
        binarySearchTree.insert(1);
        binarySearchTree.insert(4);
        binarySearchTree.insert(15);
        binarySearchTree.insert(8);
        binarySearchTree.insert(20);
        binarySearchTree.insert(0);
        binarySearchTree.insert(2);
        binarySearchTree.insert(14);

        System.out.println("删除是否成功：" + binarySearchTree.removNode(15));

        System.out.println(binarySearchTree.search(5));
        System.out.println(binarySearchTree.search(14));
    }
}
